package com.thh.article.service.impl;

import com.google.common.collect.Maps;
import com.thh.article.service.AppArticleInfoService;
import com.thh.model.article.dtos.ArticleInfoDto;
import com.thh.model.article.pojos.ApArticleConfig;
import com.thh.model.article.pojos.ApArticleContent;
import com.thh.model.article.pojos.ApAuthor;
import com.thh.model.article.pojos.ApCollection;
import com.thh.model.behavior.pojos.ApBehaviorEntry;
import com.thh.model.behavior.pojos.ApLikesBehavior;
import com.thh.model.behavior.pojos.ApUnlikesBehavior;
import com.thh.model.common.dtos.ResponseResult;
import com.thh.model.common.enums.AppHttpCodeEnum;
import com.thh.model.crawler.core.parse.ZipUtils;
import com.thh.model.mappers.app.*;
import com.thh.model.user.pojos.ApUser;
import com.thh.model.user.pojos.ApUserFollow;
import com.thh.utils.common.BurstUtils;
import com.thh.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
@SuppressWarnings("all")
public class AppArticleInfoServiceImpl implements AppArticleInfoService {

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;//文章内容
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;//文章config

    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;//行为enrry

    @Autowired
    private ApUnlikesBehaviorMapper apUnlikesBehaviorMapper;//不喜欢行为

    @Autowired
    private ApLikesBehaviorMapper apLikesBehaviorMapper;//喜欢行为

    @Autowired
    private ApCollectionMapper apCollectionMapper;//收藏

    @Autowired
    private ApUserFollowMapper apUserFollowMapper;//用户Follow

    @Autowired
    private ApAuthorMapper apAuthorMapper;//作者
    /**
     * 加载文章详情内容
     * @param articleId
     * @return
     */
    public ResponseResult getArticleInfo(Integer articleId){
        // 参数无效
        if(articleId==null||articleId<1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据文章id查询config的信息
        ApArticleConfig config = apArticleConfigMapper.selectByArticleId(articleId);
        Map<String,Object> data = new HashMap<>();
        // 参数无效
        if(config==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }else if(!config.getIsDelete()){
            // 没删除的标识才返回给客户端
            ApArticleContent apArticleContent = apArticleContentMapper.selectByArticleId(articleId);
            String content = ZipUtils.gunzip(apArticleContent.getContent());
            apArticleContent.setContent(content);
            data.put("content",apArticleContent);
        }
        data.put("config",config);
        return ResponseResult.okResult(data);
    }


    /**
     * 加载文章详情的初始化配置信息，比如关注、点赞、不喜欢、阅读位置等
     *
     * 通过用户id或者设备id  ------>entryId
     *
     * 下面四张表 行为表 为了在前端显示出来  默认是boolean类型的false
     *
     * 点赞表
     * 不喜欢表
     * 收藏表
     *关注表(先查作者  然后通过userId查)
     *
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        // 用户和设备不能同时为空
        if(user==null&& dto.getEquipmentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = null;
        if(user!=null){
            userId = user.getId();
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.selectByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        // 行为实体找以及注册了，逻辑上这里是必定有值得，除非参数错误
        if(apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        boolean isUnLike=false,isLike=false,isCollection=false,isFollow=false;
        String burst = BurstUtils.groudOne(apBehaviorEntry.getId());

        // 判断是否是已经不喜欢
        ApUnlikesBehavior apUnlikesBehavior = apUnlikesBehaviorMapper.selectLastUnLike(apBehaviorEntry.getId(),dto.getArticleId());
        if(apUnlikesBehavior!=null&&apUnlikesBehavior.getType()==ApUnlikesBehavior.Type.UNLIKE.getCode()){
            isUnLike=true;
        }
        // 判断是否是已经点赞
        ApLikesBehavior apLikesBehavior = apLikesBehaviorMapper.selectLastLike(burst,apBehaviorEntry.getId(),dto.getArticleId(), ApCollection.Type.ARTICLE.getCode());
        if(apLikesBehavior!=null&&apLikesBehavior.getOperation()==ApLikesBehavior.Operation.LIKE.getCode()){
            isLike=true;
        }
        // 判断是否收藏
        ApCollection apCollection = apCollectionMapper.selectForEntryId(burst,apBehaviorEntry.getId(),dto.getArticleId(),ApCollection.Type.ARTICLE.getCode());
        if(apCollection!=null){
            isCollection=true;
        }
        //是否关注
        ApAuthor apAuthor = apAuthorMapper.selectById(dto.getAuthorId());
        if(user!=null&&apAuthor!=null&&apAuthor.getUserId()!=null) {
            ApUserFollow apUserFollow = apUserFollowMapper.selectByFollowId(BurstUtils.groudOne(user.getId()), user.getId(), apAuthor.getUserId().intValue());
            if (apUserFollow != null) {
                isFollow = true;
            }
        }

        Map<String,Object> data = Maps.newHashMap();
        data.put("isfollow",isFollow);//点赞关注
        data.put("islike",isLike);//喜欢
        data.put("isunlike",isUnLike);//不喜欢
        data.put("iscollection",isCollection);//收藏

        return ResponseResult.okResult(data);
    }
}